# -*-coding:utf-8-*-
import sys
import time
from selenium.webdriver.common.by import By
from config.globalconfig import picture_login_path
from public.common.basepage import Page


class LoginPage(Page):
    url = "http://120.24.81.195/qrweb/admin/login/index.html"

    def __init__(self, url=url):
        super().__init__(url=url)
        self.username = (By.ID, "name")
        self.password = (By.ID, "pwd")
        self.submit = (By.ID, "submit")
        # 验证码未处理
        self.sec = None

    def login_username(self, username):
        self.get_element(*self.username).send_keys(username)

    def login_password(self, password):
        self.get_element(*self.password).send_keys(password)

    def login_picture(self):
        if not self.sec:
            pass
        else:
            time.sleep(self.sec)

    def login_submit(self):
        self.get_element(*self.submit).click()

    def user_login(self, username=None, password=None):
        self.text_clear(*self.username)
        self.text_clear(*self.password)
        self.login_username(username)
        self.login_password(password)
        self.login_picture()
        self.login_submit()

    def get_text(self, username=None, password=None):
        self.user_login(username, password)
        text = self.get_alter_text()
        self.dirver.switch_to.alert.accept()
        func_name = sys._getframe().f_back.f_code.co_name
        self.save_picture(picture_login_path, func_name)
        return text

    # def get_cookie(self):
    #     #     self.open()
    #     #     self.sec = 4
    #     #     self.user_login(username="gdsa", password="admin")
    #     #     cookies = self.dirver.get_cookies()
    #     #     name = cookies[0]["name"]
    #     #     value = cookies[0]["value"]
    #     #     return name, value
